package com.demo.utils.groovy

/**
 * 计算公式父类，运行计算脚本时会extend该class，在这里可以添加工具方法。
 */
abstract class ComputingBaseUtil extends Script{

	/**
	 * 取绝对值
	 * @param num
	 * @return absnum
	 */
	def abs(num){
		def absNum=null;
		if (num instanceof BigDecimal) {
			absNum = ((BigDecimal) num).abs();
		} else if (num instanceof Integer) {
			absNum = Math.abs((int) num);
		} else if (num instanceof Double) {
			absNum = Math.abs((double) num);
		} else if (num instanceof Long) {
			absNum = Math.abs((long) num);
		} else if (num instanceof Float) {
			absNum = Math.abs((float) num);
		}
		return absNum;
	}


	def test(){
		return "test";
	}

	/**
	 * 取较小值
	 * @param num1,num2
	 * @return num1和num2的较小值
	 */
	def min(num1,num2){
		if (num1 instanceof BigDecimal && num2 instanceof BigDecimal) {
			minNum = num1.min(num2);
		} else if(num1 instanceof Integer && num2 instanceof BigDecimal) {
			minNum = (num1 as BigDecimal).min(num2);
		} else if(num1 instanceof BigDecimal && num2 instanceof Integer) {
			minNum = (num2 as BigDecimal).min(num1);
		} else {
			minNum = Math.min(num1,num2);
		}
		return minNum;
	}

	/**
	 * 取较大值
	 * @param num1,num2
	 * @return num1和num2的较大值
	 */
	def max(num1,num2){
		if (num1 instanceof BigDecimal && num2 instanceof BigDecimal) {
			maxNum = num1.max(num2);
		} else if(num1 instanceof Integer && num2 instanceof BigDecimal) {
			maxNum = (num1 as BigDecimal).max(num2);
		} else if(num1 instanceof BigDecimal && num2 instanceof Integer) {
			maxNum = (num2 as BigDecimal).max(num1);
		} else {
			maxNum = Math.max(num1,num2);
		}
		return maxNum;
	}

	/**
	 * 数组和一个数相乘
	 * @param array,num
	 * @return array中每个数和num相乘
	 */
	def mul(array,num){
		List c= new ArrayList()
		if(array!=null){
			array.each{
				c<<(it as BigDecimal) * (num as BigDecimal)
			}
		}
		return c;
	}

}